package com.demo.sdexplorerdemo;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SDFileExplorer extends Activity {

    TextView txt = null;
    ListView list = null;
    File current;
    File[] currentFiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
        setContentView(R.layout.file_explorer);
        txt = (TextView) findViewById(R.id.text);
        list = (ListView) findViewById(R.id.list);

        // SD卡
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_LONG).show();
            return;
        }
        current = Environment.getExternalStorageDirectory();
        currentFiles = current.listFiles();
        inflateListView();
        //
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                // TODO Auto-generated method stub
                if (currentFiles[position].isFile()) {
                    return;
                }
                current = currentFiles[position];
                currentFiles = current.listFiles();
                inflateListView();
            }

        });
        //返回上一级
        Button btn = (Button) findViewById(R.id.parent);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    if (current.equals(
                            Environment.getExternalStorageDirectory())) {
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                current = current.getParentFile();
                currentFiles = current.listFiles();
                inflateListView();
            }
        });
    }

    // 给list绑定Adapter
    private void inflateListView() {
        try {
            txt.setText("当前目录：" + current.getCanonicalPath());

            ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < currentFiles.length; i++) {
                Map<String, Object> item = new HashMap<String, Object>();
                if (currentFiles[i].isDirectory()) {
                    item.put("icon", R.drawable.folder);
                } else {
                    item.put("icon", R.drawable.file);
                }
                item.put("name", currentFiles[i].getName());
                data.add(item);
            }
            SimpleAdapter adapter = new SimpleAdapter(this, data,
                    R.layout.line, new String[]{"icon", "name"}, new int[]{
                    R.id.line_image, R.id.line_text});
            list.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
